import React, { PureComponent } from 'react';
import {
  Button,
  Modal, Table,
} from 'antd';
import { connect } from 'dva';

const confirm = Modal.confirm;

@connect(({ Apply, loading }) => ({
  Apply,
}))

class DescView extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      selectedItem: [],
      selectedRowKeys: [],
    };
  }

  handleWithdraw = () => {
    const { selectedItem } = this.state;
    const { dispatch, selectedRows,handleLookDesc } = this.props;
    let arr = selectedItem.map((item) => {
      return item.loanNo;
    });
    confirm({
      title: `确定`,
      content: `你确定提现?`,
      okText: '确认',
      cancelText: '取消',
      onOk() {
        dispatch({
          type: 'Apply/withdraw',
          payload: {
            enterpriseNo: selectedRows.enterpriseNo,
            loanNoList: arr,
          },
          callback(res) {
            window.open(res.msg.url);
            handleLookDesc(false, [])
          },
        });
      },
      onCancel() {
        console.log('Cancel');
      },
    });
  };

  render() {

    const { lookDescView, handleLookDesc, dataSource } = this.props;
    const { selectedRowKeys } = this.state;
    const onSelectChange = (selectedRowKeys, item) => {
      this.setState({ selectedRowKeys });
      this.setState({ selectedItem: item });
    };

    const rowSelection = {
      selectedRowKeys,
      onChange: onSelectChange,
    };
    const columns = [
      {
        title: '融资金额',
        dataIndex: 'amount',
        key: 'amount',
      },
      {
        title: '编号',
        dataIndex: 'loanNo',
        key: 'loanNo',
      },

      {
        title: '募集金额',
        dataIndex: 'raiseAmount',
        key: 'raiseAmount',
      },
      {
        title: '已提现金额',
        dataIndex: 'withdrawAmount',
        key: 'withdrawAmount',
      },
      {
        title: '可提现金额',
        dataIndex: 'withdraw',
        key: 'withdraw',
      },
      {
        title: '起息日',
        dataIndex: 'interestTime',
        key: 'interestTime',
      }];
    return (
      <Modal
        width={800}
        destroyOnClose
        title="放款记录"
        visible={lookDescView}
        onOk={() => handleLookDesc(false, [])}
        onCancel={() => handleLookDesc(false, [])}
        footer={[
          selectedRowKeys.length>0?
          <Button type="primary" onClick={this.handleWithdraw}>提现</Button>:"",
          <Button key="back" onClick={() => handleLookDesc(false, [])}>返回</Button>,
          <Button key="submit" type="primary" onClick={() => handleLookDesc(false, [])}>
            确定
          </Button>,
        ]}>
        <Table
          rowSelection={rowSelection}
          pagination={false}
          dataSource={dataSource ? dataSource : []}
          columns={columns}
        />
      </Modal>
    );
  }

}

export default DescView;
